package com.wiim.processor;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;

import com.wiim.connector.RestClient;
import com.wiim.model.ExchangeAwardData;
import com.wiim.model.ShopData;
import com.wiim.model.TransactionLogData;

import android.util.Log;

public class SHOPProcessor extends BaseModuleProcessor {

	private final static String URL_GET_SHOPLIST_BY_CATEGORYID = "shop/getlistbycategoryid/%s?format=json";
	private final static String URL_GET_SHOP_BY_QRCODE = "shop/getlistbyqrcode/%s&%s?format=json";
	private final static String URL_ADD_DO_POINTSTRANSACTION = "shop/dopointtransaction/%s&%s&%s&%s&%s&%s&%s&%s?format=json";
	private final static String URL_GET_GET_EXCHANGEAWARD_LIST = "shop/exchangeaward/list?format=json";
	
	private final static String URL_GET_TRANSACTION_HISTORY_LIST = "shop/gettransactionhistory/%s?format=json";
	

	//set category = * if you want to get all shop
	public ShopData[] getShopList(String categoryid) {
		HttpEntity<?> requestEntity = createGETRequestEntity();
		ShopData[] res = null;
		try {
			RestClient client = new RestClient();
			String url = String.format(URL_GET_SHOPLIST_BY_CATEGORYID,categoryid);

			ResponseEntity<ShopData[]> response = client.template().exchange(
					RestClient.apiUrl(url), HttpMethod.GET, requestEntity,
					ShopData[].class);
			res = response.getBody();
		} catch (Exception ex) {
			Log.i("getShopList", ex.getMessage());
		}
		return res;
	}
	
	public ExchangeAwardData[] getExchangeAwardList() {
		HttpEntity<?> requestEntity = createGETRequestEntity();
		ExchangeAwardData[] res = null;
		try {
			RestClient client = new RestClient();
			String url = String.format(URL_GET_GET_EXCHANGEAWARD_LIST);

			ResponseEntity<ExchangeAwardData[]> response = client.template().exchange(
					RestClient.apiUrl(url), HttpMethod.GET, requestEntity,
					ExchangeAwardData[].class);
			res = response.getBody();
		} catch (Exception ex) {
			Log.i("getExchangeAwardList", ex.getMessage());
		}		
		return res;
	}
	
	//set SHOPID = "NONE" if you want to get ShopData by QR_CODE
	public ShopData getShopDetailByQrcodeOrShopId(String qrcode,String shopid){
		HttpEntity<?> requestEntity = createGETRequestEntity();
		ShopData res = null;
		try {
			RestClient client = new RestClient();
			String url = String.format(URL_GET_SHOP_BY_QRCODE, qrcode, shopid);

			ResponseEntity<ShopData> response = client.template().exchange(
					RestClient.apiUrl(url), HttpMethod.GET, requestEntity,
					ShopData.class);
			res = response.getBody();
		} catch (Exception ex) {
			Log.i("getShopDetailByQrcode", ex.getMessage());
		}		
		return res;
	}
	
	public String addRewardPoints(String transactiontype, String userid, String fullname, String shopid, String shopname, String rewardid, String exchangeawardid, String verifycode){
		HttpEntity<?> requestEntity = createGETRequestEntity();
		String res = "";
		try {
			RestClient client = new RestClient();
			String url = String.format(URL_ADD_DO_POINTSTRANSACTION,transactiontype, userid, fullname, shopid, shopname, rewardid, exchangeawardid, verifycode);

			ResponseEntity<String> response = client.template().exchange(
					RestClient.apiUrl(url), HttpMethod.GET, requestEntity,String.class);
			res = response.getBody();
		} catch (Exception ex) {
			Log.i("addRewardPoints", ex.getMessage());
		}
		return res;
	}
	
	public TransactionLogData[] getTransactionHistoryByUserid(String userid) {
		HttpEntity<?> requestEntity = createGETRequestEntity();
		TransactionLogData[] res = null;
		try {
			RestClient client = new RestClient();
			String url = String.format(URL_GET_TRANSACTION_HISTORY_LIST,userid);

			ResponseEntity<TransactionLogData[]> response = client.template().exchange(
					RestClient.apiUrl(url), HttpMethod.GET, requestEntity,
					TransactionLogData[].class);
			res = response.getBody();
		} catch (Exception ex) {
			Log.i("getTransactionHistoryByUserid", ex.getMessage());
		}
		return res;
	}
}
